<?php
// utility functions
//
// digiboard by digitalboom.org
// http://www.digitalboom.org
//
// Information Technology program
// Sirindhorn International Institute of Technology
// Thammasat University, Rangsit Campus
// http://www.siit.tu.ac.th
//
// first created on: 2002 Jun xx
// last modified on: 2004 Apr 28

include_once("settings/config.inc.php");
include_once("settings/forum.inc.php");
include_once("settings/accounts.inc.php");

// connect to the database of digiboard
function dgb_db_connect() {
 global $DGB;
 
$db_link = @mysql_pconnect($DGB["DB_SERVER"], $DGB["DB_USER"], $DGB["DB_PASS"]);
 if ($db_link != false) {
  if (!(@mysql_select_db($DGB["DB_DBNAME"]))) {
   return false;
  }
 } else {
  return false;
 }

 return $db_link;
}

function mysql_timestamp14_to_timestamp($mysql_timestamp14) {
// MySQL TIMESTAMP(14) format is "YYYYMMDDhhmmss"
// if $mysql_timestamp14 is "00000000000000"
//   return -1
return
 mktime(
  substr($mysql_timestamp14, 8, 2), // hour
  substr($mysql_timestamp14,10, 2), // minute
  substr($mysql_timestamp14,12, 2), // second
  substr($mysql_timestamp14, 4, 2), // month
  substr($mysql_timestamp14, 6, 2), // date
  substr($mysql_timestamp14, 0, 4)  // year
 );
}

function mysql_datetime_to_timestamp($mysql_datetime) {
// MySQL DATETIME format is "YYYY-MM-DD hh:mm:ss"
// if $mysql_datetime is "0000-00-00 00:00:00"
//   return -1
return
 mktime(
  substr($mysql_datetime,11, 2), // hour
  substr($mysql_datetime,14, 2), // minute
  substr($mysql_datetime,17, 2), // second
  substr($mysql_datetime, 5, 2), // month
  substr($mysql_datetime, 8, 2), // date
  substr($mysql_datetime, 0, 4)  // year
 );
}

// make forum icon .. forum + {new|update|hot}
function forum_icon($forum_id, $read_num, $ans_num, $post_timestamp, $ans_timestamp, $now_timestamp) {
 global $DGB_FORUM_NAME, $DGB_FORUM;

//Normal Icon
$name = "images/fi/talks";
//By forum name
//$name = "images/fi/".$DGB_FORUM_NAME[$forum_id];
//X'Mas , New Year, Valentine Icon
//$name = "images/fi/xmas";
//$name = "images/fi/newyear";
//$name = "images/fi/valentine";
//$name = "images/fi/tsunami"; //Black theme
//End X'Mas , New Year

 if ((($read_num/10) + $ans_num) > $DGB_FORUM['HOT_NUMBER']) {
  $name = $name."_hot";
 } else {
  if ($now_timestamp-$post_timestamp < $DGB_FORUM['NEW_TIMEDIFF'])
   $name = $name."_new";
  elseif ($now_timestamp-$ans_timestamp < $DGB_FORUM['UPDATE_TIMEDIFF'])
   $name = $name."_update";
 }

 return $name.".gif";
}

function forum_name($forum_id) {
 global $DGB_FORUM_NAME, $DGB_FORUM;
 if (($forum_id < -1) || ($forum_id > $DGB_FORUM["NUMBER"])) {
  return "(invalid)";
 } elseif ($forum_id == -1) {
  return "all";
 } elseif ($forum_id == 0) {
  return "general";
 } else {
  return $DGB_FORUM_NAME[$forum_id];
 }
}

function time_display($timestamp, $now_timestamp) {
 global $DGB;
 if ($timestamp == "-1")
  return "-";

 $timediff = $now_timestamp-$timestamp;

 if ($timediff < 0)
  return @date($DGB["SHORT_DATE_FORMAT"],$timestamp);
 elseif ($timediff < 60)
  return "(".$timediff." secs)";
 elseif ($timediff < 3600)
  return "(".(int)($timediff/60)." mins)";
 elseif ($timediff < 86400)
  return "(".(int)($timediff/3600)." hrs)";
// elseif ($timediff < 172800)
//  return "ystrday";
 else
  return @date($DGB["SHORT_DATE_FORMAT"],$timestamp);
}

// ===========================================================
// STRING-TRANSFORMING-RELATED

// This function filters out all HTML
function strip_html($text) {
 $text = eregi_replace("\"","&quot;",$text);
 $text = eregi_replace("<","&lt;",$text);
 $text = eregi_replace(">","&gt;",$text);
 return $text;
}

// This function filters out HTML based on the level given
function parse_html($text,$level=0) {
 if($level==0) { //*** Remove all HTML
  $text = strip_html($text);
 } elseif($level==1) { //*** Remove all but the most basic formatting HTML
  $text = eregi_replace("<([^>]*)>","``\\1%%",$text);
  $text = eregi_replace("``(/?(b|i|u|p|br|ul|ol|li|code|sub|sup|small|big|strong|center))%%","<\\1>",$text);
  $text = eregi_replace("``([^(%%)]*)%%","",$text);
 } elseif($level==2) { //*** Removes scripting and other 'unsafe' tag
  $text=eregi_replace("</?(style|div|span|layer|html|bgsound|body|meta|applet|embed|iframe|script|object)([^>\n]*)>", "", $text);
  $text=eregi_replace("(onload|onmouseout|onmouseover|onerror)=[\"']([^\n]*)['\"]", "", $text);
 }
 return $text;
}

// !!!! NEED TO DO RECODE !!!!!
// - cover more cases
// - use less resource (e.g. CPU time)
function bbcodeparse($text) {
 // do text format
 $text=eregi_replace(quotemeta("[b]"), quotemeta("<b>"), $text);
 $text=eregi_replace(quotemeta("[/b]"),quotemeta("</b>"),$text);
 $text=eregi_replace(quotemeta("[i]"), quotemeta("<i>"), $text);
 $text=eregi_replace(quotemeta("[/i]"),quotemeta("</i>"),$text);
 $text=eregi_replace(quotemeta("[u]"), quotemeta("<u>"), $text);
 $text=eregi_replace(quotemeta("[/u]"),quotemeta("</u>"),$text);
 $text=eregi_replace(quotemeta("[sup]"), quotemeta("<sup>"), $text);
 $text=eregi_replace(quotemeta("[/sup]"),quotemeta("</sup>"),$text);
 $text=eregi_replace(quotemeta("[sub]"), quotemeta("<sub>"), $text);
 $text=eregi_replace(quotemeta("[/sub]"),quotemeta("</sub>"),$text);

// $text=eregi_replace("([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])","<a href=\"\\1://\\2\\3\" target=\"\\2\\3\">\\1://\\2\\3</a>",$text);
// $text= eregi_replace("([[:alnum:]]+)@([^[:space:]]*)([[:alnum:]])","<a href=mailto:\\1@\\2\\3>\\1@\\2\\3</a>",$text);

 // do [www]xxx[/www]
 $text=eregi_replace("\\[url\\]www.([^\\[]*)\\[/url\\]","<a class=\"linkBtn2\" href=\"http://www.\\1\" target=\"_blank\">www.\\1</a>",$text);
 $text=eregi_replace("\\[url\\]([^\\[]*)\\[/url\\]","<a class=\"linkBtn2\" href=\"\\1\" target=\"_blank\">\\1</a>",$text);
 $text=eregi_replace("\\[www\\]www.([^\\[]*)\\[/www\\]","<a class=\"linkBtn2\" href=\"http://www.\\1\" target=\"_blank\">www.\\1</a>",$text);
 $text=eregi_replace("\\[www\\]([^\\[]*)\\[/www\\]","<a class=\"linkBtn2\" href=\"\\1\" target=\"_blank\">\\1</a>",$text);

 // do [mail]xxx[/mail]
 $text=eregi_replace("\\[email\\]([^\\[]*)\\[/email\\]","<a class=\"linkBtn2\" href=\"mailto:\\1\">\\1</a>",$text);
 $text=eregi_replace("\\[mail\\]([^\\[]*)\\[/mail\\]","<a class=\"linkBtn2\" href=\"mailto:\\1\">\\1</a>",$text);

// mask email from spam (replace @ with AT, . with DOT)
 $text = maskemail($text);

 // do quotes
 $text=eregi_replace("quote\\]","quote]",$text);  // make lower case
 $text=str_replace("[quote]\r\n","<blockquote><smallfont>quote:</smallfont><hr>",$text);
 $text=str_replace("[quote]","<blockquote><smallfont>quote:</smallfont><hr>",$text);
 $text=str_replace("[/quote]\r\n","<hr></blockquote>",$text);
 $text=str_replace("[/quote]","<hr></blockquote>",$text);

 // do [img]xxx[/img]
 $text=eregi_replace("\\[img\\]([^\\[]*)\\[/img\\]","<img src=\"\\1\" border=\"0\">",$text);
 
 return $text;
}


// convert smiley code to smiley image
function smileycodeparse($text) {
 //$fp = fopen("smilies/smilies_set.txt", "r");
 $smiley = array (
  "(&)",
  ":-)",
  ":)",
  ":-D",
  ":-d",
  ":D",
  ":d",
  ":-O",
  ":-o",
  ":O",
  ":o",
  ":-P",
  ":-p",
  ":P",
  ":p",
  ";-)",
  ";)",
  ":-(",
  ":(",
  ":-S",
  ":-s",
  ":S",
  ":s",
  ":-|",
  ":|",
  ":'(",
  ":-$",
  ":$",
  ":-@",
  ":@",
  "(H)",
  "(h)",
  "(A)",
  "(a)",
  "(6)",
  "(M)",
  "(m)",
  "(X)",
  "(x)",
  "(Z)",
  "(z)",
  "(})",
  "({)",
  "(K)",
  "(k)",
  "(L)",
  "(l)",
  "(U)",
  "(u)",
  "(F)",
  "(f)",
  "(W)",
  "(w)",
  "(G)",
  "(g)",
  "(^)",
  "(C)",
  "(c)",
  "(B)",
  "(b)",
  "(D)",
  "(d)",
  "(T)",
  "(t)",
  "(E)",
  "(e)",
  "(O)",
  "(o)",
  "(P)",
  "(p)",
  "(~)",
  "(I)",
  "(i)",
  "(S)",
  "(s)",
  "(*)",
  "(8)",
  "(@)",
  ":-[",
  ":[",
  "(Y)",
  "(y)",
  "(N)",
  "(n)",
  "(r)",
  "(R)",
  "(#)",
  "(o,,o)",
  "(- -x)",
  "(- -z)",
  "(O o)",
  "(^-^)",
  "(*^-^*)",
  "(+)",
  "(x x)",
  "[[freek]]",
  "t(-_-)",
  "[[ha]]",
  "[[haha]]",
  "[[ouch]]",
  "[[inlove]]",
  "[[mafia]]",
  "(O_O)",
  "[[sly]]",
  "(-_-')",
  "('-_-)",
  "[[good]]",
  "(>-<)",
  "(- -)",
  "(O_o)",
  "(O o?)",
  "(- -\")",
  "(-\"-)",
  "(O\"o)",
  "[[heehee]]",
  "(\$_\$)",
  "[[alien]]",
  "(T-T)",
  "[[bloody]]"
 );
 
 $smiley_img = array (
  "<img src=\"smilies/messenger/dog.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/smile.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/smile.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/sm_open.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/sm_open.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/sm_open.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/sm_open.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/sm_o.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/sm_o.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/sm_o.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/sm_o.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/sm_tongue.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/sm_tongue.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/sm_tongue.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/sm_tongue.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/sm_wink.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/sm_wink.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/sm_sad.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/sm_sad.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/sm_confuse.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/sm_confuse.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/sm_confuse.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/sm_confuse.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/sm_stem.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/sm_stem.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/sm_cry.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/sm_embarrass.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/sm_embarrass.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/sm_angry.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/sm_angry.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/sm_sun.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/sm_sun.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/sm_angel.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/sm_angel.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/sm_devil.gif\" height=19 width=19>",
  
  "<img src=\"smilies/messenger/people.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/people.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/girl.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/girl.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/boy.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/boy.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/girl_hug.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/boy_hug.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/kiss.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/kiss.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/heart.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/heart.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/heart_broken.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/heart_broken.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/rose.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/rose.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/rose_wilted.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/rose_wilted.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/gift.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/gift.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/cake.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/coffee.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/coffee.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/beer.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/beer.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/martini.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/martini.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/phone.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/phone.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/envelope.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/envelope.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/clock.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/clock.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/camera.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/camera.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/film.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/bulb.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/bulb.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/moon.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/moon.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/star.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/music.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/cat.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/bat.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/bat.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/thmb_up.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/thmb_up.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/thmb_dn.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/thmb_dn.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/rainbow.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/rainbow.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/sun.gif\" height=19 width=19>",

 "<img src=\"smilies/messenger/skull.gif\" height=19 width=19>",
 "<img src=\"smilies/messenger/sm_angry2.gif\" height=19 width=19>",
 "<img src=\"smilies/messenger/sm_asleep.gif\" height=19 width=19>",
 "<img src=\"smilies/messenger/sm_bewilder.gif\" height=19 width=19>",
 "<img src=\"smilies/messenger/sm_cuteb.gif\" height=19 width=19>",
 "<img src=\"smilies/messenger/sm_cuteg.gif\" height=19 width=19>", 
 "<img src=\"smilies/messenger/sm_cyclops.gif\" height=19 width=19>",
 "<img src=\"smilies/messenger/sm_dead.gif\" height=19 width=19>",
 "<img src=\"smilies/messenger/sm_freek.gif\" height=19 width=19>",
 "<img src=\"smilies/messenger/sm_fyou.gif\" height=19 width=19>",
 "<img src=\"smilies/messenger/sm_grin.gif\" height=19 width=19>",
 "<img src=\"smilies/messenger/sm_grin2.gif\" height=19 width=19>",
 "<img src=\"smilies/messenger/sm_hammered.gif\" height=19 width=19>",
 "<img src=\"smilies/messenger/sm_inlove.gif\" height=19 width=19>",
 "<img src=\"smilies/messenger/sm_mafia.gif\" height=19 width=19>",
 "<img src=\"smilies/messenger/sm_shocked.gif\" height=19 width=19>",
 "<img src=\"smilies/messenger/sm_sly.gif\" height=19 width=19>",
 "<img src=\"smilies/messenger/sm_sweat.gif\" height=19 width=19>",
 "<img src=\"smilies/messenger/sm_sweat2.gif\" height=19 width=19>",
 "<img src=\"smilies/messenger/sm_thumbs.gif\" height=19 width=19>",
 "<img src=\"smilies/messenger/sm_tight.gif\" height=19 width=19>",
 "<img src=\"smilies/messenger/sm_tired.gif\" height=19 width=19>",
 "<img src=\"smilies/messenger/sm_uh.gif\" height=19 width=19>",
 "<img src=\"smilies/messenger/sm_what.gif\" height=19 width=19>",
 "<img src=\"smilies/messenger/sm_what2.gif\" height=19 width=19>",
 "<img src=\"smilies/messenger/sm_what3.gif\" height=19 width=19>",
 "<img src=\"smilies/messenger/sm_what4.gif\" height=19 width=19>",
 "<img src=\"smilies/messenger/sm_x.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/sm_money.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/alien.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/sm_cry2.gif\" height=19 width=19>",
  "<img src=\"smilies/messenger/sm_blood.gif\" height=19 width=19>"
 );

 $text=str_replace($smiley, $smiley_img, $text);
 
 return $text;
}

// *** SPECIAL ***  special purpose uses of string conversion
// convert [RegisterUserLogin] string to registerd user shower
function registeruser($text) {
 $regist = array("[RegisterUserLogin]");
 $regist_img = array("<sup><font size=\"-2\">&nbsp;&reg;</font></sup>");
 $text = str_replace($regist, $regist_img, $text);

 return $text;
}

function maskemail($text) {
// replace @ in email with AT
 $text=eregi_replace("([[:alnum:]\\._-]+)@([[:alnum:]\\.-]+)", "\\1 AT \\2", $text);
 return $text;
}

// does text contains ++++++ or $$$$$$ or those kind of things?
function isnonsense($text) {
 $len = strlen($text);
 $lastcharsymb = 0;

 $symb1 = array("$", "&#3647;", "=", "+", "-", "*", "/", "\\", "#", "&", "@", "%", "~", "{", "}", "<", ">", "^", ":", ";", "|"); 
 $symb2 = array("(", ")", "[", "]", "!", "?"); 

 for ($i = 0; $i < $len; $i++) {
  if (! ctype_space($text[$i])) {

   if (in_array($text[$i], $symb1)) {
    $lastcharsymb += 4;
   } else
   if (in_array($text[$i], $symb2)) {
    $lastcharsymb += 1;
   } else {
    $lastcharsymb = 0;
   }

  if ($lastcharsymb >= 15) {
    return 1;
  }
  }
 }

 return 0;
}

?>